home *** CD-ROM | disk | FTP | other *** search
- Path: solon.com!not-for-mail
- From: seebs@solutions.solon.com (Peter Seebach)
- Newsgroups: comp.lang.c,comp.lang.c++,comp.lang.perl
- Subject: Re: Stupid array problems
- Date: 14 Jan 1996 21:54:44 -0600
- Organization: Usenet Fact Police (Undercover)
- Message-ID: <4dcj64$ome@solutions.solon.com>
- References: <4d9b9v$14n@paperboy.ids.net> <4dbfd1$2hpc@news.gate.net> <4dc5fv$qdr@newsreader.wustl.edu>
- NNTP-Posting-Host: solutions.solon.com
-
- In article <4dc5fv$qdr@newsreader.wustl.edu>,
- Krishnamoorthy Lakshminarayan <kln@howdy.wustl.edu> wrote:
- > void delete_array_element (char **array, //The array of strings
- > int max, // total strings in array
- > int num_del) //index you want deleted
- > {
- > for (int i=max-1; i>num_del; i++) //max-1, because you've deleted
- > //one element
- > {
- > array[i] = array[i-1];
- > }
- > }
-
- HUH?
-
- 1. This is clearly C++, and the question is in comp.lang.c and
- comp.lang.perl.
- 2. The reason i would start at max - 1 is not that you've deleted one
- element, but because the top element in a 10 element array is array[9].
- 3. Let's try your code on a trivial case: A 3 element array, delete
- the middle one. So...
- x = { 1, 2, 3 };
- delete(x, 3, 1)
- i = 2
- x[2] = x[1];
- inc i -> i = 3
- ...
- Hmm. Looks like you're going backwards. Lets count the other way...
- after x[2] = x[1];
- i = 1;
- break (i no longer < num_del)
- ...
- We end up with
- x = { 1, 2, 2 };
- ... Looks like this *copies* the element.
-
- Please check solutions before posting them, and use a language appropriate
- to at least one of the groups. C and C++ are *NOT* the same language, and
- frequently have vastly different semantics for seemingly identical things.
-
- -s
- --
- Peter Seebach - seebs@solon.com - Copyright 1995 Peter Seebach.
- C/Unix proto-wizard -- C/Unix questions? Send mail for help. No, really!
- Using trn? Weird new newsgroup problem? I know the fix! Email me!
- The *other* C FAQ - ftp taniemarie.solon.com /pub/c/afq - Not A Flying Toy
-